<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>注册 - 学生成绩管理系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700,300' rel='stylesheet'>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/animate.css">

    <style>
        body {
            background: #f5f5f5 !important;
            font-family: 'Open Sans', Arial, sans-serif;
            color: #333;
        }
        .register-container {
            max-width: 600px;
            margin: 50px auto;
            padding: 30px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .register-title {
            font-size: 24px;
            text-align: center;
            margin-bottom: 30px;
            color: #3498db;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        .btn-submit {
            width: 100%;
            padding: 12px;
            background: #2ecc71;
            border: none;
            color: white;
            font-size: 16px;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.3s;
        }
        .btn-submit:hover {
            background: #27ae60;
        }
        .login-link {
            text-align: center;
            margin-top: 20px;
        }
        .login-link a {
            color: #3498db;
            text-decoration: none;
        }
        .login-link a:hover {
            text-decoration: underline;
        }
        .user-type-group {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
        }
        .user-type-label {
            flex: 1;
            text-align: center;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin: 0 5px;
            cursor: pointer;
            transition: all 0.3s;
        }
        .user-type-label:hover {
            background: #f0f0f0;
        }
        .user-type-label input[type="radio"] {
            margin-right: 5px;
        }
        .validation-message {
            margin-top: 5px;
            font-size: 14px;
        }
        .validation-success {
            color: green;
        }
        .validation-error {
            color: red;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <h1 class="register-title">用户注册</h1>

        <form id="registerForm">
            <div class="form-group">
                <label class="form-label">用户类型</label>
                <div class="user-type-group">
                    <label class="user-type-label">
                        <input type="radio" name="userType" value="student" checked> 学生
                    </label>
                    <label class="user-type-label">
                        <input type="radio" name="userType" value="teacher"> 教师
                    </label>
                </div>
            </div>

            <div class="form-group">
                <label for="username" class="form-label">用户名（学号/工号）</label>
                <input type="text" id="username" class="form-control" placeholder="请输入用户名" required>
                <div id="usernameValidation" class="validation-message"></div>
            </div>

            <div class="form-group">
                <label for="password" class="form-label">密码</label>
                <input type="password" id="password" class="form-control" placeholder="请输入密码(至少5位)" required minlength="5">
            </div>

            <div class="form-group">
                <label for="confirmPassword" class="form-label">确认密码</label>
                <input type="password" id="confirmPassword" class="form-control" placeholder="请再次输入密码" required>
            </div>

            <div class="form-group">
                <label for="realName" class="form-label">真实姓名</label>
                <input type="text" id="realName" class="form-control" placeholder="请输入真实姓名" required>
            </div>

            <button type="submit" class="btn-submit">立即注册</button>
        </form>
    </div>

    <script src="js/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            // 用户名实时验证
            $('#username').on('input', function() {
                var username = $(this).val();
                var validationElement = $('#usernameValidation');

                if (username.length === 0) {
                    validationElement.text('');
                    return;
                }

                validationElement.text('验证中...').removeClass('validation-success validation-error');

                $.ajax({
                    url: 'check_username.php',  // 修改为你的后端验证接口
                    method: 'POST',
                    data: { username: username },
                    dataType: 'json',
                    success: function(response) {
                        if (response.valid) {
                            validationElement.text('用户名可用').addClass('validation-success').removeClass('validation-error');
                        } else {
                            validationElement.text('用户名已存在').addClass('validation-error').removeClass('validation-success');
                        }
                    },
                    error: function() {
                        validationElement.text('验证失败，请稍后再试').addClass('validation-error').removeClass('validation-success');
                    }
                });
            });

            // 表单提交
            $('#registerForm').submit(function(e) {
                e.preventDefault();

                var userType = $('input[name="userType"]:checked').val();
                var username = $('#username').val();
                var password = $('#password').val();
                var confirmPassword = $('#confirmPassword').val();
                var realName = $('#realName').val();

                if (password !== confirmPassword) {
                    alert('两次输入的密码不一致！');
                    return;
                }

                if ($('#usernameValidation').text() !== '用户名可用') {
                    alert('请确认用户名是否有效！');
                    return;
                }

                var registerData = {
                    userType: userType,
                    username: username,
                    password: password,
                    realName: realName
                };

                console.log('注册数据:', registerData);

                // 实际应通过AJAX发送到后端处理注册
                alert('注册成功！');
                window.location.href = 'Login.html';
            });
        });
    </script>
</body>
</html>
